{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @copyright 2003-2014 by the INDEPNET Development Team.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% import 'components/form/fields_macros.html.twig' as fields %}

{% set params = params|default({}) %}
{% set candedit = candedit|default(item.maySolve()) %}
{% set can_read_kb = can_read_kb|default(
    has_profile_right('knowbase', constant('READ')) or has_profile_right('knowbase', constant('KnowbaseItem::READFAQ'))
) %}
{% set can_update_kb = can_update_kb|default(has_profile_right('knowbase', constant('UPDATE'))) %}
{% set nokb = nokb|default(params['nokb'] is defined or params['nokb'] == true) %}
{% set rand = rand|default(random()) %}
{% set formoptions = formoptions|default(params['formoptions'] ?? '') %}

{# In some cases, we may already have an HTML form (massive actions) #}
{% set add_html_form = no_form is not defined or no_form == false %}

<div class="itiltask">
    {% if add_html_form %}
        <form
            name="asset_form"
            style="width: 100%;"
            class="d-flex flex-column"
            method="post"
            action="{{ subitem.getFormURL() }}"
            enctype="multipart/form-data"
            data-track-changes="true"
            data-submit-once {{ formoptions|raw }}
        >
    {% endif %}

    <input type="hidden" name="itemtype" value="{{ item.getType() }}" />
    <input type="hidden" name="{{ item.getForeignKeyField() }}" value="{{ item.fields['id'] }}" />

    {{ call_plugin_hook('pre_item_form', {"item": subitem, 'options': params}) }}

    <div class="row mx-n3 mx-xxl-auto">
        <div class="col-12 col-xl-7 col-xxl-8">
            {{ fields.textareaField(
                'content',
                subitem.fields['content'],
                '',
                {
                    'full_width': true,
                    'no_label': true,
                    'enable_richtext': true,
                    'enable_fileupload': true,
                    'enable_mentions': true,
                    'entities_id': item.fields['entities_id'],
                    'rand': rand,
                    'editor_height': 300,
                }
            ) }}
        </div>
        <div class="col-12 col-xl-5 col-xxl-4 order-first order-md-last pe-0 pe-xxl-auto">
            <div class="row">
                {% set task_template_lbl %}
                    <i
                        class="{{ 'TaskTemplate'|itemtype_icon }} me-1"
                        title="{{ _n('Task template', 'Task templates', get_plural_number()) }}"
                    ></i>
                {% endset %}
                {{ fields.dropdownField(
                    'TaskTemplate',
                    'tasktemplates_id',
                    subitem.fields['tasktemplates_id'],
                    task_template_lbl,
                    {
                        'full_width': true,
                        'icon_label': true,
                        'on_change': 'itiltasktemplate_update' ~ rand ~ '(this.value)',
                        'entity': item.fields['entities_id'],
                        'rand': rand,
                    }
                ) }}

                {% set task_date_lbl %}
                    <i
                        class="ti ti-calendar me-1"
                        title="{{ _n('Date', 'Dates', 1) }}"
                    ></i>
                {% endset %}
                {{ fields.datetimeField(
                    'date',
                    subitem.fields['date'],
                    task_date_lbl,
                    {
                        'full_width': true,
                        'icon_label': true,
                        'rand': rand,
                    }
                ) }}

                {# Category #}
                {% set task_category_lbl %}
                    <i class="ti ti-tag me-1" title="{{ _n('Category', 'Categories', 1) }}"></i>
                {% endset %}
                {{ fields.dropdownField(
                    'TaskCategory',
                    'taskcategories_id',
                    subitem.fields['taskcategories_id'],
                    task_category_lbl,
                    {
                        'full_width': true,
                        'icon_label': true,
                        'entity': item.fields['entities_id'],
                        'condition': {
                            'is_active': 1,
                        },
                        'rand': rand,
                    }
                ) }}

                {# Status #}
                {% set task_state_lbl %}
                    <i class="ti ti-list-check me-1" title="{{ __('Status') }}"></i>
                {% endset %}

                {% set task_state %}
                    {% do call('Planning::dropdownState', ['state', subitem.fields['state'], true, {'rand': rand}]) %}
                {% endset %}

                {{ fields.htmlField(
                    'state',
                    task_state,
                    task_state_lbl,
                    {
                       'full_width': true,
                       'icon_label': true,
                       'rand': rand,
                    }
                ) }}

                {% set task_private_lbl %}
                    <i class="ti ti-eye-off me-1" title="{{ __('Private') }}"></i>
                {% endset %}
                {{ fields.sliderField(
                    'is_private',
                    subitem.fields['is_private'],
                    task_private_lbl,
                    {
                        'full_width': true,
                        'icon_label': true,
                        'rand': rand,
                        'additional_attributes': {
                            'onchange': 'toggleTimelinePrivate(this.checked, this)',
                        }
                    }
                ) }}

                {% if candedit and can_update_kb and not nokb %}
                {% set task_to_kb_lbl %}
                    <i
                        class="ti ti-device-floppy me-1"
                        title="{{ __('Save and add to the knowledge base') }}"
                        data-bs-toggle="tooltip"
                        data-bs-placement="top"
                    ></i>
                {% endset %}
                {{ fields.sliderField(
                    '_task_to_kb',
                    0,
                    task_to_kb_lbl,
                    {
                        'full_width': true,
                        'icon_label': true,
                        'rand': rand,
                    }
                ) }}
                {% endif %}

                {# Duration #}
                {% set task_actiontime_lbl %}
                    <i class="ti ti-stopwatch me-1" title="{{ __('Duration') }}"></i>
                {% endset %}
                {{ fields.dropdownTimestampField('actiontime', subitem.fields['actiontime'], task_actiontime_lbl, {
                    'full_width': true,
                    'icon_label': true,
                    'rand': rand,
                    'min': 0,
                    'max': 8 * constant('HOUR_TIMESTAMP'),
                    'addfirstminutes': true,
                    'inhours': true,
                    'toadd': range(9, 100)|map(i => i * constant('HOUR_TIMESTAMP')),
                }) }}

                {# User #}
                {% set task_user_lbl %}
                    <i class="ti ti-user me-1" title="{{ 'User'|itemtype_name }}"></i>
                {% endset %}
                {{ fields.dropdownField(
                    'User',
                    'users_id_tech',
                    subitem.fields['users_id_tech']|default(session('glpiID')),
                    task_user_lbl,
                    {
                        'full_width': true,
                        'icon_label': true,
                        'entity': item.fields['entities_id'],
                        'right': 'own_ticket',
                        'rand': rand,
                    }
                ) }}

                {# Group #}
                {% set task_group_lbl %}
                    <i class="ti ti-users me-1" title="{{ 'Group'|itemtype_name }}"></i>
                {% endset %}
                {{ fields.dropdownField(
                    'Group',
                    'groups_id_tech',
                    subitem.fields['groups_id_tech'],
                    task_group_lbl,
                    {
                        'full_width': true,
                        'icon_label': true,
                        'entity': item.fields['entities_id'],
                        'condition': {'is_task': 1},
                        'rand': rand,
                    }
                ) }}

                <script type="text/javascript">
                    function showPlanUpdate{{ rand }}(e) {
                        $('#plan{{ rand }}').hide();
                        $('#viewplan{{ rand }}').load('{{ path('/ajax/planning.php') }}', {
                            action: "add_event_classic_form",
                            form: "followups", // Was followups for tasks before. Can't find where this is used.
                            entity: {{ item.fields.entities_id }},
                            rand_user: {{ random() }},
                            rand_group: {{ random() }},
                            itemtype: "{{ subitem.type }}",
                            items_id: {{ subitem.fields.id|default(-1) }},
                            parent_itemtype: "{{ item.getType() }}",
                            parent_items_id: {{ item.fields.id is empty ? 0 : item.fields.id }},
                            parent_fk_field: "{{ item.getForeignKeyField() }}",
                            begin: "{{ subitem.fields['begin'] }}",
                            end: "{{ subitem.fields['end'] }}",
                        });
                    }
                </script>
                <div class="col-12">
                {% if subitem.can(subitem.fields['id'], constant('UPDATE')) and subitem.fields['begin'] %}
                    <script type="text/javascript">
                        showPlanUpdate{{ rand }}();
                    </script>
                    <button id="unplan{{ rand }}" class="btn btn-outline-warning" type="submit" name="unplan"
                            onclick="return confirm('{{ __('Confirm the deletion of planning?') }}');">
                        <i class="fas ti ti-calendar-off"></i>
                        <span>{{ __('Unplan') }}</span>
                    </button>
                {% else %}
                    <button id="plan{{ rand }}" class="btn btn-outline-secondary text-truncate" onclick="showPlanUpdate{{ rand }}()" type="button">
                        <i class="ti ti-calendar"></i>
                        <span>{{ __('Plan this task') }}</span>
                    </button>
                {% endif %}
                <div id="viewplan{{ rand }}"></div>
                </div>
            </div>
        </div>
    </div>

    {% if disable_pending_reasons is not defined or disable_pending_reasons == false %}
        {% set pending_reasons %}
            {% set show_pending_reasons_actions = item.fields['status'] == constant('CommonITILObject::WAITING') and not has_pending_reason %}
            {% if call('PendingReason_Item::canDisplayPendingReasonForItem', [subitem]) %}
                <span
                    class="input-group-text bg-yellow-lt py-0 pe-0 {{ show_pending_reasons_actions ? 'flex-fill' : '' }}"
                    id="pending-reasons-control-{{ rand }}"
                >
                    <span
                        class="d-inline-flex align-items-center"
                        title="{{ __("Set the status to pending") }}"
                        data-bs-toggle="tooltip"
                        data-bs-placement="top"
                        role="button"
                    >
                        <i class="ti ti-player-pause-filled me-2"></i>
                        <label class="form-check form-switch mt-2">
                        <input type="hidden" name="pending" value="0"/>
                        <input
                            type="checkbox"
                            name="pending"
                            value="1"
                            class="form-check-input"
                            id="enable-pending-reasons-{{ rand }}"
                            role="button"
                            {{ item.fields['status'] == constant('CommonITILObject::WAITING') ? 'checked' : '' }}
                            data-bs-toggle="collapse"
                            data-bs-target="#pending-reasons-setup-{{ rand }}"
                        />
                        </label>
                    </span>

                    {% if not has_pending_reason %}
                        <div
                            class="collapse ps-2 py-1 flex-fill {{ show_pending_reasons_actions ? 'show' : '' }}"
                            aria-expanded="{{ show_pending_reasons_actions ? 'true' : 'false' }}"
                            id="pending-reasons-setup-{{ rand }}"
                        >
                            {{ include('components/itilobject/timeline/pending_reasons.html.twig') }}
                        </div>
                    {% endif %}
                </span>
            {% endif %}
        {% endset %}
    {% endif %}

    {{ call_plugin_hook('post_item_form', {"item": subitem, 'options': params}) }}
    {# Fixed min-height ensure no height increase when toggling the pending reason button, as select 2 dropdown are a bit higher than the default footer height #}
    <div class="d-flex card-footer mx-n3 mb-n3 flex-wrap" style="row-gap: 10px; min-height: 79px">
        {% if subitem.fields['id'] <= 0 %}
            {# Do not enable flex wrapping when creating a new item as the pending form will be merged with the add button in an input group #}
            <div class="input-group flex-nowrap">
                <button class="btn btn-primary" type="submit" name="add">
                <i class="ti ti-plus"></i>
                <span>{{ _x('button', 'Add') }}</span>
                </button>
                {% if disable_pending_reasons is not defined or disable_pending_reasons == false %}
                    {{ pending_reasons|raw }}
                {% endif %}
            </div>
        {% else %}
            <input type="hidden" name="id" value="{{ subitem.fields['id'] }}" />
            <button class="btn btn-primary me-2" type="submit" name="update">
                <i class="ti ti-device-floppy"></i>
                <span>{{ _x('button', 'Save') }}</span>
            </button>

            {% if subitem.can(subitem.fields['id'], constant('PURGE')) %}
                <button class="btn btn-outline-danger me-2" type="submit" name="purge"
                        onclick="return confirm('{{ __('Confirm the final deletion?') }}');">
                <i class="ti ti-trash"></i>
                <span>{{ _x('button', 'Delete permanently') }}</span>
                </button>
            {% endif %}
            {% if disable_pending_reasons is not defined or disable_pending_reasons == false %}
                {{ pending_reasons|raw }}
            {% endif %}
        {% endif %}
    </div>

    {% if add_html_form %}
        <input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}" />
        </form>
    {% endif %}
</div>
